<script lang="ts" setup>
import {ref,defineEmits} from "vue";
import $kt from "/src/components/kantboot"
import { useSettingStore } from "../storeMoudules/setting.ts";

let settingStore = useSettingStore();
let emits = defineEmits(["submit"]);

let dialogVisible = ref(false);

let requestParamInit: any = {
  phone: "",
  email: "",
  password: "",
  orgId: null,
  realName: "",
  roleCode:null,
}

let requestParam = ref<any>(
    JSON.parse(JSON.stringify(requestParamInit))
);

let init = () => {

  requestParam.value = JSON.parse(JSON.stringify(requestParamInit));
}


let save = () => {
  $kt.request.request({
    url: "/system-user-web/admin/user/saveOfDtu",
    method: "post",
    data: requestParam.value,
    stateSuccess: (res: any) => {
      dialogVisible.value = false;
      emits("submit");
      init();
    }
  });
};



let open = (param?: any) => {
  if (param) {
    requestParam.value = JSON.parse(JSON.stringify(param));
  }else{
    requestParam.value = JSON.parse(JSON.stringify(requestParamInit));
  }

  dialogVisible.value = true;
};

let orgList = ref([]);

let getOrgList = () => {
  $kt.request.request({
    url: "/system-user-web/admin/org/getAll",
    method: "post",
    stateSuccess: (res: any) => {
      orgList.value = res.data;
    }
  });
}

getOrgList();

let roleList = ref([]);

let getRoleList = () => {
  $kt.request.request({
    url: "/system-user-web/admin/role/getAll",
    method: "post",
    stateSuccess: (res: any) => {
      roleList.value = res.data;
    }
  });
}

getRoleList();


defineExpose({
  open,
});

</script>

<template>
  <el-dialog title="保存" v-model="dialogVisible"
             width="30%">
    <el-form :model="requestParam"
             label-position="top"
             label-width="80px">
      <el-form-item label="手机号">
        <el-input v-model="requestParam.phone"></el-input>
      </el-form-item>
      <el-form-item label="邮箱">
        <el-input v-model="requestParam.email"></el-input>
      </el-form-item>
      <el-form-item label="密码">
        <el-input v-model="requestParam.password"></el-input>
      </el-form-item>
      <el-form-item label="姓名">
        <el-input v-model="requestParam.realName"></el-input>
      </el-form-item>
      <el-form-item label="组织">
        <el-select v-model="requestParam.orgId" placeholder="请选择组织">
          <el-option
              v-for="item in orgList"
              :key="item.id"
              :label="item.name"
              :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="角色">
        <el-select v-model="requestParam.roleCode" placeholder="请选择角色">
          <el-option
              v-for="item in roleList"
              :key="item.code"
              :label="item.name"
              :value="item.code">
          </el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="dialogVisible = false">取 消</el-button>
      <el-button type="primary" @click="save">确 定</el-button>
    </div>
  </el-dialog>
</template>

<style scoped>
.dialog-footer {
  text-align: right;
}
</style>